<?php

namespace app\services\order;

use app\services\BaseServices;
use app\services\pc\OrderServices;
use app\services\product\product\StoreProductServices;
use app\services\product\product\SyncStoreProductServices;
use app\services\product\sku\StoreProductAttrValueServices;

class SyncOrderServices extends BaseServices
{


    public function syncOrderV1($orderId){
        /** @var StoreOrderServices $storeOrderServices **/
        $storeOrderServices = app()->make(StoreOrderServices::class);
        /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices **/
        $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
        /** @var StoreProductServices $storeProductServices **/
        $storeProductServices = app()->make(StoreProductServices::class);
        /** @var SyncStoreProductServices $syncStoreProductServices **/
        $syncStoreProductServices = app()->make(SyncStoreProductServices::class);
        /** @var StoreProductAttrValueServices $storeProductAttrValueServices **/
        $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
        $order = $storeOrderServices->get($orderId);
        if (!$order) {
            return false;
        }

        $params = [];
        $price = 0;

        // 佰亿千禧同步订单只能是一个商品，不是一个商品代表不需要同步
        if (count($order['cart_id']) > 1) {
            return true;
        }

        $cartId = $order['cart_id'][0];
        $storeOrderCartInfo = $storeOrderCartInfoServices->getOne(['cart_id' => $cartId]);
        $product = $storeProductServices->getOne(['id' => $storeOrderCartInfo['product_id']]);
        // 如果不是佰亿千禧同步订单，则不需要同步
        if ($product['sync_type'] != 1)
            return true;
        $params = $syncStoreProductServices->getParams([
            "outOrderId" => $order['order_id'],
            'uuid' => $order['mark'] == "" ? $order['user_phone'] : $order['mark'],
            'amount' => $storeOrderCartInfo['cart_num'],
            'itemId' => $product['item_id'],
            'callbackUrl' => 'https://ttxzgz.gzsoft.fun/api/order/syncCallbackOrderV1'
        ]);
        $storeProductAttrValue = $storeProductAttrValueServices->getOne(['product_id' => $product['id'], 'type' => 0]);
        $price = $storeOrderCartInfo['cart_num'] * $storeProductAttrValue['cost'];

        $shopMoney = $this->getProductShopMoney($syncStoreProductServices->getParams([]));
        if ($price > $shopMoney)
            return false;

        $response = $this->submitOrder($params);
        if ($response['code'] == '00') {
            $storeOrderServices->update($order['id'], ['sync_order_id' => $response['orderId'], 'sync_msg' => $response['msg'], 'sync_type' => 0 ]);
            return true;
        }
        else {
            $storeOrderServices->update($order['id'], ['sync_msg' => $response['msg'], 'sync_type' => 0]);
            return false;
        }
    }

    /**
     * 获取平台余额
     * @param $params
     * @return int|void
     */
    public function getProductShopMoney($params){
        $url = "http://123.56.251.130:8911/api/account/balance?$params";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
        curl_close($ch);
        $response = mb_convert_encoding($response, 'UTF-8', 'auto');
        $response = json_decode($response, true);
        // TODO 获取失败,走失败记录流程
        if ($response['code'] != "00") {
            return 0;
        }

        return $response['balance'] + $response['creditQuota'];
    }

    public function submitOrder($params){
        $url = "http://123.56.251.130:8911/api/order/submit?$params";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
        curl_close($ch);
        $response = mb_convert_encoding($response, 'UTF-8', 'auto');
        $response = json_decode($response, true);
        return $response;
    }


    public function getBYQXOrder($orderId) {
        /** @var SyncStoreProductServices  $syncStoreProductServices **/
        $syncStoreProductServices = app()->make(SyncStoreProductServices::class);
        /** @var StoreOrderServices $storeOrderServices **/
        $storeOrderServices = app()->make(StoreOrderServices::class);
        $order = $storeOrderServices->getOne(['order_id' => $orderId]);
        $params = $syncStoreProductServices->getParams([
            "orderId" => $order['sync_order_id'],
            "outOrderId" => $order['order_id']
        ]);

        $url = "http://123.56.251.130:8911/api/order/query?$params";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
        curl_close($ch);
        $response = mb_convert_encoding($response, 'UTF-8', 'auto');
        $response = json_decode($response, true);
        if ($response['code'] == "00") {
            $storeOrderServices->update($order['id'], ['sync_type' => $response['orderStatus'], 'sync_msg' => $response['orderDesc']]);
        }
    }

}